﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Silverlight.Sample
{
    public class DataViewModel
    {
        public ObservableCollection<Model> Datas { get; set; }
        public ObservableCollection<Model2> Datas2 { get; set; }
        private bool canUpdate = true;

        public bool CanUpdate
        {
            get { return canUpdate; }
            set { canUpdate = value; }
        }

        private TimeSpan updateSpeed = TimeSpan.FromMilliseconds(10);

        public TimeSpan UpdateSpeed
        {
            get { return updateSpeed; }
            set
            {

                updateSpeed = value;
                timer.Interval = updateSpeed;
            }
        }

        public int MaxData { get; set; }


        public DataViewModel()
        {
            Datas2 = new ObservableCollection<Model2>();
            Datas2.Add(new Model2("2000", 200, 100));
            Datas2.Add(new Model2("2001", 210, 90));
            Datas2.Add(new Model2("2002", 232, 150));
            Datas2.Add(new Model2("2003", 199, 130));
            Datas2.Add(new Model2("2004", 100, 69));
            Datas2.Add(new Model2("2005", 150, 110));
            Datas2.Add(new Model2("2006", 130, 110));
            Datas2.Add(new Model2("2007", 170, 99));

            Datas = new ObservableCollection<Model>();
            Datas.Add(new Model("2000", 200));
            Datas.Add(new Model("2001", 210));
            Datas.Add(new Model("2002", 232));
            Datas.Add(new Model("2003", 199));
            Datas.Add(new Model("2004", 100));
            Datas.Add(new Model("2005", 150));
            Datas.Add(new Model("2006", 130));
            Datas.Add(new Model("2007", 170));

            timer.Tick += timer_Tick;
            timer.Interval = updateSpeed;
            timer.Start();
            MaxData = 8;
        }

        DispatcherTimer timer = new DispatcherTimer();
        int i =0;
        Random rand = new Random();
        void timer_Tick(object sender, EventArgs e)
        {
            if (CanUpdate)
            {
                if (Datas.Count > MaxData)
                    Datas.RemoveAt(0);
                Datas.Add(new Model("20" + i.ToString(), rand.Next(100, 200)));
                i++;
            }
        }

    }
}
